package com.cxyd.modules.wms.controller;

import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.LocateDTO;
import com.cxyd.modules.wms.service.LocateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 货位信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/locate")
public class LocateController {
    private final LocateService locateService;

    public LocateController(LocateService locateService) {
        this.locateService = locateService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:locate:page")
    public Result<PageData<LocateDTO>> page(@RequestParam Map<String, Object> params){
        PageData<LocateDTO> page = locateService.page(params);

        return new Result<PageData<LocateDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:locate:info")
    public Result<LocateDTO> get(@PathVariable("id") Long id){
        LocateDTO data = locateService.get(id);

        return new Result<LocateDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:locate:save")
    public Result save(@RequestBody LocateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        locateService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:locate:update")
    public Result update(@RequestBody LocateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        locateService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:locate:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        locateService.delete(ids);

        return new Result();
    }

    @PostMapping("forbid/{id}")
    @LogOperation("禁用")
    public Map<String,Object> freeze(@PathVariable("id") Long locateId){
        UserDetail user = SecurityUser.getUser();
        //效验数据
        final Map<String,Object> map =new HashMap<>();
        map.put("locateId",locateId);
        map.put("userId",user.getId());
        locateService.locateDisabled(map);
        return map;
    }

    @PostMapping("lock")
    @LogOperation("禁用/解禁")
    public Result lock(@RequestBody Map<String, Object> params) {

        ProcResult procResult = locateService.lock(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());

    }
}